﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SharePointEnforcer.Rules
{
    /// <summary>
    /// Checks to make sure that the sites use explicit permissions
    /// </summary>
    public class InheritMasterPage : EnforcerRuleBase
    {
        public override void Evaluate(EnforcerArg pArg)
        {
            pArg.Result = EnforcerEvaluateResult.Ignore;

            // this check does not apply for the root web
            if (!pArg.IsRootWeb)
            {
                if (string.Compare(pArg.Web.CustomMasterUrl, pArg.Site.RootWeb.CustomMasterUrl, false) != 0 ||
                    string.Compare(pArg.Web.MasterUrl, pArg.Site.RootWeb.MasterUrl, false) != 0)
                {
                    pArg.Result = EnforcerEvaluateResult.Fail;
                    pArg.RepairState = EnforcerRepairResult.RepairRequired;
                } 
                else
                    pArg.Result = EnforcerEvaluateResult.Pass;
            }
        }
        public override void Repair(EnforcerArg pArg)
        {
            pArg.Web.CustomMasterUrl = pArg.Site.RootWeb.CustomMasterUrl;
            pArg.Web.MasterUrl = pArg.Site.RootWeb.MasterUrl;
            pArg.RepairState = EnforcerRepairResult.RepairCompleted;
        }
    }
}
